/**
 * Copyright 2023 Han-Kuiwei
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hkw.daterangepicke;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.hkw.daterangepicke.configure.PickerOptions;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateDialog extends Dialog implements View.OnClickListener {

    private final String TAG = getClass().getSimpleName();
    private static String endDate;

    private static String startDate;

    private MyListener listener;

    private TextView tvDateEnd;
    private TextView tvDialogCancel;
    private TextView tvDialogConfirm;
    private TextView tvDialogTitle;

    private RelativeLayout rv_top_bar;
    private TextView tvDateStart;
    private int type;
    private View vDateEnd;
    private View vDateStart;

    private DateTimeWheelLayout wheelLayout;

    protected PickerOptions mPickerOptions;

    //public DateDialog(Context paramContext) {
    public DateDialog(PickerOptions pickerOptions) {
        //super(paramContext, R.style.dialog_bottom);

        super(pickerOptions.context, R.style.dialog_bottom);
        mPickerOptions = pickerOptions;

        requestWindowFeature(1);
        setContentView(R.layout.dialog_date);
        tvDateEnd = findViewById(R.id.tvDialogDateEnd);
        tvDateStart = findViewById(R.id.tvDialogDateStart);
        vDateEnd = findViewById(R.id.vDialogDateEnd);
        vDateStart = findViewById(R.id.vDialogDateStart);
        wheelLayout = findViewById(R.id.wheelLayout);

        LinearLayout linDateStart = findViewById(R.id.linDialogDateStart);
        LinearLayout linDateEnd = findViewById(R.id.linDialogDateEnd);

        tvDialogCancel = findViewById(R.id.tvDialogCancel);
        tvDialogConfirm = findViewById(R.id.tvDialogConfirm);
        tvDialogTitle = findViewById(R.id.tvDialogTitle);
        rv_top_bar = findViewById(R.id.rv_top_bar);

        //tvDateEnd.setOnClickListener(this);
        //tvDateStart.setOnClickListener(this);
        tvDialogCancel.setOnClickListener(this);
        tvDialogConfirm.setOnClickListener(this);

        linDateStart.setOnClickListener(this);
        linDateEnd.setOnClickListener(this);

        startDate = "";
        endDate = "";


        //设置文字
        tvDialogConfirm.setText(TextUtils.isEmpty(mPickerOptions.textContentConfirm) ? mPickerOptions.context.getResources().getString(R.string.pickerview_submit) : mPickerOptions.textContentConfirm);
        tvDialogCancel.setText(TextUtils.isEmpty(mPickerOptions.textContentCancel) ? mPickerOptions.context.getResources().getString(R.string.pickerview_cancel) : mPickerOptions.textContentCancel);
        tvDialogTitle.setText(TextUtils.isEmpty(mPickerOptions.textContentTitle) ? mPickerOptions.context.getResources().getString(R.string.pickerview_title) : mPickerOptions.textContentTitle);

        //设置color
        tvDialogConfirm.setTextColor(mPickerOptions.textColorConfirm);
        tvDialogCancel.setTextColor(mPickerOptions.textColorCancel);
        tvDialogTitle.setTextColor(mPickerOptions.textColorTitle);
        rv_top_bar.setBackgroundColor(mPickerOptions.bgColorTitle);

        //设置文字大小
        tvDialogConfirm.setTextSize(mPickerOptions.textSizeSubmitCancel);
        tvDialogCancel.setTextSize(mPickerOptions.textSizeSubmitCancel);
        tvDialogTitle.setTextSize(mPickerOptions.textSizeTitle);


        this.tvDateEnd.setText(endDate);
        //this.wheelLayout.setDefaultValue(DateTimeEntity.today());
        this.wheelLayout.setSelectedTextColor(getContext().getResources().getColor(R.color.black));
        this.wheelLayout.setSelectedTextSize((getContext().getResources().getDisplayMetrics()).scaledDensity * 16.0F);
        this.wheelLayout.setTextColor(getContext().getResources().getColor(R.color.color_999));
        this.wheelLayout.setTextSize((getContext().getResources().getDisplayMetrics()).scaledDensity * 14.0F);
        this.wheelLayout.setIndicatorColor(getContext().getResources().getColor(R.color.transparent));
        //this.wheelLayout.setOnDateTimeSelectedListener(new DateDialog$$ExternalSyntheticLambda0(this));
        this.wheelLayout.setOnDateTimeSelectedListener(new OnDateTimeSelectedListener() {
            @Override
            public void onDateSelected(int paramInt1, int paramInt2, int paramInt3) {
                if (type == 0) {
                    startDate = paramInt1 + "-" + paramInt2 + "-" + paramInt3;
                    tvDateStart.setText(startDate);
                } else {
                    endDate = paramInt1 + "-" + paramInt2 + "-" + paramInt3;
                    tvDateEnd.setText(endDate);
                }
            }
        });
        showDialog();
    }

    private void setRange(String paramString1, String paramString2, String paramString3) {
        DateTimeEntity dateTimeEntity1;
        DateTimeEntity dateTimeEntity2;
        DateTimeEntity dateTimeEntity3;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(paramString1);
            /*if (mPickerOptions.isNowEndDate) {
                if (date != null) {
                    dateTimeEntity1 = new DateTimeEntity();
                    //this();
                    int year00 = date.getYear();
                    Log.d(TAG, "year00=" + year00);
                    if (this.type == 1) {
                        dateTimeEntity1.setYear(year00 + 1900);
                    } else {
                        dateTimeEntity1.setYear(year00 + 1890);
                    }
                    if (date.getMonth() == 0) {

                        dateTimeEntity1.setMonth(1);
                    } else {
                        dateTimeEntity1.setMonth(date.getMonth() + 1);

                    }
                    dateTimeEntity1.setDay(date.getDate());

                } else {
                    dateTimeEntity1 = DateTimeEntity.yearOnFuture(-10);
                }
            } else {*/
                Calendar d = Calendar.getInstance();
                d.setTime(date);
                if (date != null) {

                    //this();
                    int year0 = d.get(Calendar.YEAR);
                    Log.d(TAG, "year0=" + year0);
                   /* if (this.type == 1) {
                        Log.d(TAG, "this.type == 1");
                        dateTimeEntity1 = DateTimeEntity.yearOnFuture(10);
                    } else {
                        dateTimeEntity1 = DateTimeEntity.yearOnFuture(-10);
                    }*/

                    dateTimeEntity1 = new DateTimeEntity();
                    dateTimeEntity1.setYear(year0);

                    if (d.get(Calendar.MONTH) == 0) {

                        dateTimeEntity1.setMonth(1);
                    } else {

                        dateTimeEntity1.setMonth(d.get(Calendar.MONTH) + 1);
                    }

                    dateTimeEntity1.setDay(d.get(Calendar.DAY_OF_MONTH));
                } else {
                    dateTimeEntity1 = DateTimeEntity.yearOnFuture(-10);
                }
            //}

        } catch (Exception exception) {
            exception.printStackTrace();

            dateTimeEntity1 = DateTimeEntity.yearOnFuture(-10);
        }
        try {
            Date date1 = simpleDateFormat.parse(paramString2);

            /*if (mPickerOptions.isNowEndDate) {
                if (date1 != null) {
                    dateTimeEntity2 = new DateTimeEntity();
                    //this();
                    dateTimeEntity2.setYear(date1.getYear() + 1900);
                    if (date1.getMonth() == 0) {
                        dateTimeEntity2.setMonth(1);
                    } else {
                        dateTimeEntity2.setMonth(date1.getMonth() + 1);
                    }
                    dateTimeEntity2.setDay(date1.getDate());
                } else {
                    dateTimeEntity2 = DateTimeEntity.today();
                }
            } else {*/
                Calendar d1 = Calendar.getInstance();
                d1.setTime(date1);
                if (date1 != null) {
                    /*dateTimeEntity2 = new DateTimeEntity();

                    dateTimeEntity2.setYear(d1.get(Calendar.YEAR));

                    if (d1.get(Calendar.MONTH) == 0) {
                        dateTimeEntity2.setMonth(1);
                    } else {
                        dateTimeEntity2.setMonth(d1.get(Calendar.MONTH) + 1);
                    }

                    dateTimeEntity2.setDay(d1.get(Calendar.DAY_OF_MONTH));*/

                    if (this.type == 1) {
                        Log.d(TAG, "this.type == 1");
                        dateTimeEntity2 = DateTimeEntity.yearOnFuture(10);
                    } else {
                        dateTimeEntity2 = DateTimeEntity.yearOnFuture(-10);
                    }
                } else {
                    if (mPickerOptions.isNowEndDate) {
                        dateTimeEntity2 = DateTimeEntity.today();
                    } else {

                        dateTimeEntity2 = DateTimeEntity.yearOnFuture(10);
                    }
                }
            //}

        } catch (Exception exception) {
            exception.printStackTrace();
            Log.d(TAG, "date1, Exception");
            if (mPickerOptions.isNowEndDate) {
                dateTimeEntity2 = DateTimeEntity.today();
            } else {

                dateTimeEntity2 = DateTimeEntity.yearOnFuture(10);
            }
        }
        try {
            Date date2 = simpleDateFormat.parse(paramString3);


          /*  if (mPickerOptions.isNowEndDate) {
                if (date2 != null) {
                    dateTimeEntity3 = new DateTimeEntity();
                    //this();

                    dateTimeEntity3.setYear(date2.getYear() + 1900);
                    if (date2.getMonth() == 0) {
                        dateTimeEntity3.setMonth(1);
                    } else {
                        dateTimeEntity3.setMonth(date2.getMonth() + 1);
                    }
                    dateTimeEntity3.setDay(date2.getDate());
                } else {
                    dateTimeEntity3 = DateTimeEntity.today();
                }
            } else {*/
                Calendar d2 = Calendar.getInstance();
                d2.setTime(date2);
                if (date2 != null) {
                    dateTimeEntity3 = new DateTimeEntity();

                    dateTimeEntity3.setYear(d2.get(Calendar.YEAR));

                    if (d2.get(Calendar.MONTH) == 0) {
                        dateTimeEntity3.setMonth(1);
                    } else {
                        dateTimeEntity3.setMonth(d2.get(Calendar.MONTH) + 1);
                    }
                    dateTimeEntity3.setDay(d2.get(Calendar.DAY_OF_MONTH));
                } else {
                    dateTimeEntity3 = DateTimeEntity.today();
                }
            //}

        } catch (Exception exception) {
            exception.printStackTrace();
            dateTimeEntity3 = DateTimeEntity.today();
        }
        DateTimeWheelLayout dateTimeWheelLayout = this.wheelLayout;
        if (dateTimeWheelLayout != null)
            dateTimeWheelLayout.setRange(dateTimeEntity1, dateTimeEntity2, dateTimeEntity3);
    }

    /*public void showDialog(Context paramContext,*//* String paramString1, String paramString2, String paramString3,*//* MyListener paramMyListener) {
        //startDate = paramString1;
        //endDate = paramString2;

        DateDialog dateDialog = new DateDialog(paramContext);

        dateDialog.setRange(paramString1, paramString2, paramString3);
        dateDialog.setRange("", "", "");
        dateDialog.setListener(paramMyListener);
        dateDialog.getWindow().setLayout(-1, -2);
        dateDialog.show();
    }*/

    public void showDialog() {
        this.setRange("", "", "");
        //this.setListener(paramMyListener);
        this.getWindow().setLayout(-1, -2);
        this.show();
    }


    public void setListener(MyListener paramMyListener) {
        this.listener = paramMyListener;
    }

    public void show() {
        super.show();
        getWindow().setGravity(80);
        getWindow().setWindowAnimations(R.style.dialog_anim);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        //MyListener myListener;
        if (id == R.id.tvDialogConfirm) {
            if (TextUtils.isEmpty(startDate)) {
                Toast.makeText(getContext(), "请选择开始时间", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(endDate)) {
                Toast.makeText(getContext(), "请选择结束时间", Toast.LENGTH_SHORT).show();
                return;
            }
           /* myListener = this.listener;
            if (myListener != null)
                myListener.onClick(startDate, endDate);*/
            if (null != mPickerOptions.optionsSelectListener) {
                mPickerOptions.optionsSelectListener.onOptionsSelect(startDate, endDate);
            }
            dismiss();
        } else if (id == R.id.tvDialogCancel) {
            /*myListener = this.listener;
            if (myListener != null)
                myListener.onClick("", "");*/

            if (null != mPickerOptions.optionsSelectListener) {
                mPickerOptions.optionsSelectListener.onOptionsSelect("", "");
            }
            dismiss();
        } else if (id == R.id.linDialogDateStart) {
            if (this.type != 0) {
                this.type = 0;
                this.tvDateStart.setTextColor(getContext().getResources().getColor(R.color.blue));
                this.vDateStart.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
                this.tvDateEnd.setTextColor(getContext().getResources().getColor(R.color.color_999));
                this.vDateEnd.setBackgroundColor(getContext().getResources().getColor(R.color.color_f2));
                String str = startDate;
                setRange(str, endDate, str);
            }
        } else if (id == R.id.linDialogDateEnd) {
            if (this.type != 1) {
                this.type = 1;
                this.tvDateStart.setTextColor(getContext().getResources().getColor(R.color.color_999));
                this.vDateStart.setBackgroundColor(getContext().getResources().getColor(R.color.color_f2));
                this.tvDateEnd.setTextColor(getContext().getResources().getColor(R.color.blue));
                this.vDateEnd.setBackgroundColor(getContext().getResources().getColor(R.color.blue));

                Log.d(TAG, "R.id.linDialogDateEnd,startDate=" + startDate);
                Log.d(TAG, "R.id.linDialogDateEnd,startDate=" + endDate);
                setRange(startDate, endDate, endDate);
            }
        }


    }

    public interface MyListener {
        void onClick(String param1String1, String param1String2);
    }
}

